package org.dromara.business.service.impl;

import org.dromara.business.domain.User;
import org.dromara.business.domain.bo.UserBo;
import org.dromara.business.domain.vo.UserVo;
import org.dromara.business.mapper.UserMapper;
import org.dromara.business.service.UserService;
import org.dromara.common.core.enums.UserType;
import org.dromara.common.core.utils.MapstructUtils;
import cn.hutool.core.util.StrUtil;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 用户信息Service业务层处理
 *
 * @author zlj
 * @date 2024-11-19
 */
@RequiredArgsConstructor
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    private final UserMapper baseMapper;

    /**
     * 查询用户信息
     */
    @Override
    public UserVo queryById(Long userId) {
        return baseMapper.selectVoById(userId);
    }

    /**
     * 查询用户信息列表
     */
    @Override
    public TableDataInfo<UserVo> queryPageList(UserBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<User> lqw = buildQueryWrapper(bo);
        Page<UserVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询用户信息列表
     */
    @Override
    public List<UserVo> queryList(UserBo bo) {
        LambdaQueryWrapper<User> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<User> buildQueryWrapper(UserBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<User> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, User::getDeptId, bo.getDeptId());
        lqw.like(StrUtil.isNotBlank(bo.getUserName()), User::getUserName, bo.getUserName());
        lqw.like(StrUtil.isNotBlank(bo.getNickName()), User::getNickName, bo.getNickName());
        lqw.eq(StrUtil.isNotBlank(bo.getUserType()), User::getUserType, bo.getUserType());
        lqw.eq(StrUtil.isNotBlank(bo.getEmail()), User::getEmail, bo.getEmail());
        lqw.eq(StrUtil.isNotBlank(bo.getPhonenumber()), User::getPhonenumber, bo.getPhonenumber());
        lqw.eq(StrUtil.isNotBlank(bo.getOpenid()), User::getOpenid, bo.getOpenid());
        lqw.eq(StrUtil.isNotBlank(bo.getUnionid()), User::getUnionid, bo.getUnionid());
        lqw.eq(StrUtil.isNotBlank(bo.getSex()), User::getSex, bo.getSex());
        lqw.eq(bo.getAvatar() != null, User::getAvatar, bo.getAvatar());
        lqw.eq(StrUtil.isNotBlank(bo.getPassword()), User::getPassword, bo.getPassword());
        lqw.eq(bo.getAmount() != null, User::getAmount, bo.getAmount());
        lqw.eq(bo.getAllAmount() != null, User::getAllAmount, bo.getAllAmount());
        lqw.eq(StrUtil.isNotBlank(bo.getStatus()), User::getStatus, bo.getStatus());
        lqw.eq(StrUtil.isNotBlank(bo.getLoginIp()), User::getLoginIp, bo.getLoginIp());
        lqw.eq(bo.getLoginDate() != null, User::getLoginDate, bo.getLoginDate());
        lqw.eq(bo.getQrCode() != null, User::getQrCode, bo.getQrCode());
        lqw.eq(bo.getSuperiorId() != null, User::getSuperiorId, bo.getSuperiorId());
        lqw.eq(StrUtil.isNotBlank(bo.getWxAvatar()), User::getWxAvatar, bo.getWxAvatar());
        lqw.orderByDesc(User::getCreateTime);
        return lqw;
    }

    /**
     * 新增用户信息
     */
    @Override
    public Boolean insertByBo(UserBo bo) {
        User add = MapstructUtils.convert(bo, User.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setUserId(add.getUserId());
        }
        return flag;
    }

    /**
     * 修改用户信息
     */
    @Override
    public Boolean updateByBo(UserBo bo) {
        User update = MapstructUtils.convert(bo, User.class);
        validEntityBeforeSave(update);
        if (LoginHelper.getUserType().getUserType().equals(UserType.XCX_USER.getUserType())) {
            update.setUserId(LoginHelper.getUserId());
        }
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(User entity) {

    }

    /**
     * 批量删除用户信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {

        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
